package com.fihtdc.excelwriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Utility {
    private static Calendar         cal     = Calendar.getInstance();
    private static SimpleDateFormat sdf     = new SimpleDateFormat("yyyy-MM-dd-hhmmss");

    public static boolean           DBG     = true;
    public static final String      LOG_LOC = new File(ExcelWriter.class.getProtectionDomain()
                                                    .getCodeSource().getLocation().getPath())
                                                    .getParent()
                                                    + "/"
                                                    + sdf.format(cal.getTime())
                                                    + ".log";

    public static Element getRootElement(File file) {
        Element result = null;
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            result = document.getRootElement();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void Log(String str) {
        try {
            FileWriter fw = new FileWriter(new File(LOG_LOC), true);
            if (DBG) {
                fw.append(sdf.format(cal.getTime()) + "\n");
                DBG = false;
            }
            fw.append(str);
            System.out.print(str);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
